package com.sim.util;

import com.sim.hook.HookMain;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class SmsTool {

    private static final String DEVICE_NAME = "/dev/pts/5";
    private static final String[] regions = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};

    // 获取小板数量
    public static int getRegionNum() {
        if (HookMain.catService != null) {
            try {
                Object mMenuCmd;
                Field menuCmd;

                try {
                    menuCmd = HookMain.catService.getClass().getDeclaredField("mMenuCmd");
                    menuCmd.setAccessible(true);
                    mMenuCmd = menuCmd.get(HookMain.catService);
                } catch (Exception e) {
                    menuCmd = HookMain.catService.getClass().getSuperclass().getDeclaredField("mMenuCmd");
                    menuCmd.setAccessible(true);
                    mMenuCmd = menuCmd.get(HookMain.catService);
                }


                if (mMenuCmd != null) {
                    Field menu = mMenuCmd.getClass().getField("mMenu");
                    menu.setAccessible(true);
                    Object mMenu = menu.get(mMenuCmd);

                    if (mMenu != null) {
                        Field items = mMenu.getClass().getField("items");
                        items.setAccessible(true);
                        Object listItem = items.get(mMenu);

                        if (listItem != null) {
                            List<Object> itemList = (List) listItem;
                            LogHelper.log("itemList.size() : " + itemList.size());
                            if (itemList.size() > 2) {
                                return itemList.size() - 2;
                            }
                        } else {
                            LogHelper.log("listItem == null");
                        }
                    } else {
                        LogHelper.log("mMenu == null");
                    }
                } else {
                    LogHelper.log("mMenuCmd == null");
                }
            } catch (Exception e) {
                LogHelper.log(e);
            }
        } else {
            LogHelper.log("HookMain.catService == null");
        }

        return 0;
    }

    // 获取当前的卡位置
    public static String getCurrentSimNum() {
        if (HookMain.catService != null) {
            try {
                Object mCurrntCmd;
                Field currntCmd;

                try {
                    currntCmd = HookMain.catService.getClass().getDeclaredField("mCurrntCmd");
                    currntCmd.setAccessible(true);
                    mCurrntCmd = currntCmd.get(HookMain.catService);
                } catch (Exception e) {
                    currntCmd = HookMain.catService.getClass().getSuperclass().getDeclaredField("mCurrntCmd");
                    currntCmd.setAccessible(true);
                    mCurrntCmd = currntCmd.get(HookMain.catService);
                }

                if (mCurrntCmd != null) {
                    Field textMsg = mCurrntCmd.getClass().getField("mTextMsg");
                    textMsg.setAccessible(true);
                    Object mTextMsg = textMsg.get(mCurrntCmd);

                    if (mTextMsg != null) {
                        Field text = mTextMsg.getClass().getField("text");
                        text.setAccessible(true);
                        Object textObj = text.get(mTextMsg);
                        if (textObj != null){
                            return textObj.toString();
                        }
                    } else {
                        LogHelper.log("mTextMsg == null");
                    }
                } else {
                    LogHelper.log("mCurrntCmd == null");
                }
            } catch (Exception e) {
                LogHelper.log(e);
            }
        } else {
            LogHelper.log("HookMain.catService == null");
        }

        return "";
    }

    // 获取当前卡的iccId
    public static String getCurrentSimIccId() {
        if (HookMain.subscriptionInfo != null) {
            try {
                Method getIccId = HookMain.subscriptionInfo.getClass().getMethod("getIccId");
                getIccId.setAccessible(true);
                return getIccId.invoke(HookMain.subscriptionInfo) + "";
            } catch (Exception e) {
                LogHelper.log(e);
            }
        }else {
            LogHelper.log("HookMain.subscriptionInfo == null");
        }
        return null;
    }

    // 切换到指定小板的sim卡槽
    public static void switchSms(int regionIndex, int simIndex) {
        LogHelper.log("switchSms regionIndex : " + regionIndex + "; simIndex : " + simIndex);
        String region = regions[regionIndex];
        String simNum = regions[simIndex - 1];

        String switchRegion = String.format("AT+CUSATE=\"D3078202018190011%s\"", region);
        String switchSim = String.format("AT+STKTR=\"81030124000202828183010090011%s\"", simNum);

//        RootCmd.getInstance().execCmd(String.format("%s '%s' > %s", "echo", switchRegion, DEVICE_NAME));
//        ThreadUtil.sleep(5000);
//        RootCmd.getInstance().execCmd(String.format("%s '%s' > %s", "echo", switchSim, DEVICE_NAME));
    }

    public static String getSelectRegionCmd(int regionIndex) {
        String region = regions[regionIndex];
//        String switchRegion = String.format("AT+CUSATE=\"D3078202018190011%s\"", region);
        String switchRegion = String.format("D3078202018190011%s", region);
        return switchRegion;
    }

    public static String getSelectSimNumCmd(int simIndex) {
        String simNum = regions[simIndex - 1];
//        String switchSim = String.format("AT+STKTR=\"81030124000202828183010090011%s\"", simNum);
        String switchSim = String.format("81030124000202828183010090011%s", simNum);
        return switchSim;
    }

}
